package cl.gob.minvu.catastromobile;

import cl.gob.minvu.catastromobile.imp.ActivityImp;
import cl.gob.minvu.catastromobile.util.GenericUtil;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

/**
 * @author psep
 *
 */
public abstract class AbstractActivity extends Activity implements ActivityImp {
	
	protected SharedPreferences session;

	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		View v = getCurrentFocus();

		if (v != null
				&& (ev.getAction() == MotionEvent.ACTION_UP || ev.getAction() == MotionEvent.ACTION_MOVE)
				&& v instanceof EditText
				&& !v.getClass().getName().startsWith("android.webkit.")) {
			int scrcoords[] = new int[2];
			v.getLocationOnScreen(scrcoords);
			float x = ev.getRawX() + v.getLeft() - scrcoords[0];
			float y = ev.getRawY() + v.getTop() - scrcoords[1];

			if (x < v.getLeft() || x > v.getRight() || y < v.getTop()
					|| y > v.getBottom())
				GenericUtil.hideKeyboard(this);
		}
		return super.dispatchTouchEvent(ev);
	}
	
	/**
	 * @param editText
	 * @return
	 */
	protected String getStringEditText(EditText editText) {
		return editText.getText().toString().trim();
	}
	
	/**
	 * @param spinner
	 * @return
	 */
	protected String getStringSpinner(Spinner spinner) {
		return spinner.getSelectedItem().toString().trim();
	}
	
	/**
	 * @param spinner
	 * @return
	 */
	protected int getIdSpinner(Spinner spinner) {
		return spinner.getSelectedItemPosition();
	}
	
	/**
	 * @param msg
	 */
	protected void toastMsg(String msg){
		Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
	}
	
	/**
	 * @param clase
	 */
	protected <T> void go(Class<T> clase) {
		startActivity(new Intent(this, clase));
	}

}
